﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SharePoint.Contrib.Extensions;

namespace NUnit.SharePointContrib
{
    [TestFixture]
    public class IEnumerableExtensionsTest
    {
        List<string> elements;
        string result;
        List<string> results = new List<string>();

        [SetUp]
        public void Init()
        {
            elements = new List<string>
            {
                "First",
                "Second",
                "Third"
            };
            
        }

        [TearDown]
        public void Dispose()
        {
        }
        
        [Test]
        public void ExecuteCanFindAndReturnTheFirstElement()
        {
            var query = from s in elements
                        where s == "First"
                        select s;

            Assert.True(query.Execute(out result));
            Assert.That(result, Is.EqualTo("First"));
            Assert.That(result, Is.Not.EqualTo("xxx"));
 
        }

        [Test]
        public void ExecuteCanFindOneElement()
        {
            var query = from s in elements
                        where s == "First"
                        select s;

            Assert.True(query.Execute(out results));
            Assert.That(results.Count, Is.EqualTo(1));
            Assert.That(results.Count, Is.Not.GreaterThan(1));
        }

        [Test]
        public void ExecuteCanMultipleElements()
        {
            var query = from s in elements
                        where s == "First" || s == "Second" || s == "Third"
                        select s;

            Assert.True(query.Execute(out results));
            Assert.That(results.Count, Is.EqualTo(3));
            Assert.That(results.Count, Is.Not.GreaterThan(3));
        }

        [Test]
        public void ExecuteCanFail()
        {
            var query = from s in elements
                        where s == "xxx"
                        select s;

            Assert.False(query.Execute(out result));
            Assert.That(result, Is.Not.EqualTo("xxx"));

        }

        [Test]
        public void ExecuteWithMultipleResultsCanFail()
        {
            var query = from s in elements
                        where s == "xxx"
                        select s;

            Assert.False(query.Execute(out results));

        }

        [Test]
        public void ExecuteWithMultipleResultsCanFailAndFirstElementIsNull()
        {
            var query = from s in elements
                        where s == "xxx"
                        select s;

            Assert.False(query.Execute(out results));
            Assert.Null(results[0]);

        }
        

        [Test]
        public void ForEachWorksOnIEnumerable()
        {
            var enumerable = new List<string> { "one", "two", "three" }.AsEnumerable();

            enumerable.ForEach(s =>
                {
                    Console.WriteLine(s);
                });
        }


       
    }
}
